// define html elements
const circle = document.getElementById('circle');
const icon = document.getElementById('icon');
const lilcircles = document.getElementsByClassName("lilcircle");
const colorpicker = document.getElementById('colorpicker');
const shadowhextitle = document.getElementById('shadowhextitle');
const bghextitle = document.getElementById('bghextitle');
const copydrawer = document.getElementById('copydrawer');
const htmlcode = document.getElementById('htmlcode');
const csscode = document.getElementById('csscode');
const confirmcopy = document.getElementById('confirmcopy');
let ricon = '';
new ClipboardJS('.clippy');


//function to fill out copyable html and css
let generateCode = function (backgroundColor = null, textshadow = null) {
    let htmlstr = `<div class="iconcircle"><i class="material-icons shadowicon">${ricon}</i></div>`
    if (backgroundColor || textshadow) {
        let cssstr = `.iconcircle{display:flex;align-items:center;justify-content:center;height:80px;width:80px;background-color:${backgroundColor};overflow:hidden;border-radius:50%}.iconcircle .shadowicon{color:#fff;font-size:45px;text-shadow:${textshadow}}`
        csscode.innerHTML = cssstr;
    }
    htmlcode.innerHTML = htmlstr;
}


//when code copied, show user it was copied
let celements = document.getElementsByClassName('clippy');
for (var i = 0; i < celements.length; i++) {
    celements[i].addEventListener('click', function () {
        confirmcopy.classList.add('show');
        setTimeout(function () {
            confirmcopy.classList.remove('show');
        }, 1000);

    });
}


//add event listener to open code copy drawer onclick
copydrawer.addEventListener('click', function () {
    let drawer = document.getElementsByClassName('copycontainer')[0];
    if (drawer.classList.contains('innie')) {
        drawer.classList.remove('innie');
        drawer.classList.add('outtie');
    } else {
        drawer.classList.remove('outtie');
        drawer.classList.add('innie');
    }
});


//Grab Material Colors and Parse JSON object
let googleColors = null;
var data = null;
var xhr = new XMLHttpRequest();

xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
        googleColors = JSON.parse(this.responseText);

        //create preview swatches
        for (var color in googleColors) {
            for (var subcolor in googleColors[color]) {
                if (subcolor == '400' || subcolor == '500' || subcolor == '600') {
                    let shadowcolor = parseInt(subcolor) + 300;
                    let shadowhex = googleColors[color][shadowcolor]
                    let bgtitle = color + ' ' + subcolor;
                    let shadowtitle = color + ' ' + shadowcolor;
                    let el = document.createElement('div');
                    let htmlString = `<div class="lilcircle" style="background-color: ${googleColors[color][subcolor]};" data-shadow="${shadowhex}" data-bgtitle="${bgtitle}" data-shadowtitle="${shadowtitle}" data-color="${googleColors[color][subcolor]}"></div>`
                    el.innerHTML = htmlString.trim();
                    colorpicker.appendChild(el);
                    el.classList.add('colorp');
                }
            }
        }

        //set background of circle as color of selected cricle and shadow color
        for (var i = 0; i < lilcircles.length; i++) {
            lilcircles[i].addEventListener('click', function () {
                for (var i = 0; i < lilcircles.length; i++) {
                    lilcircles[i].classList.remove('active');
                }
                this.classList.add('active');
                circle.style['background-color'] = this.style['background-color'];

                //generate text-shadow string and add it onclick!
                let textshadowstring = '';
                for (i = 1; i < 120; i++) {
                    textshadowstring += i + 'px ' + i + 'px ' + this.getAttribute('data-shadow') + ',';
                }
                textshadowstring = textshadowstring.substring(0, textshadowstring.length - 1);
                icon.style['text-shadow'] = textshadowstring;

                //switch the titles
                shadowhextitle.innerHTML = this.getAttribute('data-shadow');
                bghextitle.innerHTML = this.getAttribute('data-color');
                shadowtitle.innerHTML = this.getAttribute('data-shadowtitle');
                bgtitle.innerHTML = this.getAttribute('data-bgtitle');

                //change the code to copy
                generateCode(this.style['background-color'], textshadowstring);

            });
        }

        //trigger click on an element
        document.getElementsByClassName('lilcircle')[Math.floor(Math.random() * 56) + 1].click();
    }
});
xhr.open("GET", "https://gist.githubusercontent.com/kawanet/a880c83f06d6baf742e45ac9ac52af96/raw/b4fbc9a730394eb977277e73cc37b60955463f21/material-colors.json");

xhr.send(data);


//grab random icon
let icons = [];
var icon_data = null;

var xhr_icon = new XMLHttpRequest();

let grabRandomIcon = function () {
    let randomicon = icons[Math.floor(Math.random() * icons.length)];
    ricon = randomicon.split(" ")[0];
    generateCode();
    return ricon;
}

xhr_icon.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
        icons = this.responseText.split("\n");
        icon.innerHTML = grabRandomIcon();
    }
});

xhr_icon.open("GET", "https://raw.githubusercontent.com/google/material-design-icons/master/iconfont/codepoints");
xhr_icon.send(icon_data);

//randomize icon again on user click
icon.addEventListener('click', function () {
    icon.innerHTML = grabRandomIcon();
});